package com.design.window;
import com.design.config.GameConfig;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class Router {
    private final ArrayList<Route> routes;
    private final JFrame frame;
    public Router(){
        this.routes = new ArrayList<>();
        this.frame = new JFrame();
        this.frame.setTitle(GameConfig.GameTitle);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = (int) screenSize.getWidth();//获得屏幕   宽
        int screenHeight = (int) screenSize.getHeight();//获得屏幕 高
        int width = 300;
        int height = 400;
        this.frame.setSize(width,height);
        this.frame.setLocation(screenWidth - width * 2,screenHeight / 2 - height / 2);
        this.frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//默认关闭
        this.frame.setResizable(false);//禁止拉伸
        this.frame.setLayout(null);
    }
    public void use(Route route){
        this.routes.add(route);
    }
    public void startLayout(){
        int size = this.routes.size();
        this.frame.setLayout(new GridLayout(1 + size * 2 ,1));
        this.frame.add(new JLabel());
        for (Route route1 : this.routes) {
            JButton button = new JButton(route1.getName());
            button.addActionListener(e->{
                this.push(route1.getHref());
            });
            this.frame.add(button);
            this.frame.add(new JLabel());
        }
        this.frame.setVisible(true);
    }
    public void push(String href){
        for (Route route : this.routes) {
            route.setVisible(false);
            if(route.getHref().equals(href)){
                route.setVisible(true);
            }
        }
    }
}
